﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using BusinessLogic.Interfaces;
using Domain;
using Domain.Entities;

namespace BusinessLogic.Implementations
{
    public class EFProduceRepository:IProduceRepository
    {
        private  EFDbContext context;
        public EFProduceRepository(EFDbContext context)
        {
            this.context = context;
        }
        public IEnumerable<Produce> GetProduces()
        {
            return context.Produces.ToList();
        }

        public Produce GetProduceById(int id)
        {
            return context.Produces.FirstOrDefault(i => i.Id == id);
        }

        public void CreateProduce(Produce produce)
        {
            context.Produces.Add(produce);
            context.SaveChanges();
        }

        public void SaveProduce(Produce produce)
        {
            if (produce.Id == 0)
            {
                context.Produces.Add(produce);
            }
            else
            {
                context.Entry(produce).State = EntityState.Modified;
            }
            context.SaveChanges();
        }

        public void DeleteProduceById(int id)
        {
            context.Produces.Remove(GetProduceById(id));
            context.SaveChanges();
        }

        public void SetDefultImageToProduces(IEnumerable<Produce> list, string img)
        {
            foreach (var p in list)
            {
                p.ProductArtUrl = img;
            }
            context.SaveChanges();
        }

    }
}
